package com.craftcomputing.client.Entry.PhotoWidget;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.HorizontalSplitPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.LoadListener;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.Window;
import com.craftcomputing.client.Listeners.EntryPanelListeners;
import com.craftcomputing.client.Listeners.PhotoWidgetListeners;
import com.craftcomputing.client.Listeners.PhotoWidgetListeners.*;


public class PhotoWidget extends Composite {
	/**
	* @wbp.parser.constructor
	*/
	public PhotoWidget(){
//		init("lightbox/close.gif, lightbox/close.gif");
		final VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);

		final HorizontalPanel horizontalPanel = new HorizontalPanel();
		verticalPanel.add(horizontalPanel);
		horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel.setSize("330px", "250px");
		horizontalPanel.setSpacing(3);

		int n = 12;
		
		final Grid grid = new Grid();
		verticalPanel.add(grid);
		grid.setSize("100%", "100%");

		int colsPerRow = 12; 
		int cols = (n-1)%colsPerRow+1; 
		if(n > 8) cols = colsPerRow;
		int rows = (n-1)/colsPerRow+1;
		grid.resize(rows, cols);

		final Label label_1 = new Label("");
		grid.setWidget(0, 0, label_1);
		label_1.setSize("16", "16");
		label_1.setStyleName("gwt-PhotoViewerButton");
		label_1.addMouseListener(new PhotoWidgetMouseListener(this, label_1, 0));

		final Label label_2 = new Label("");
		grid.setWidget(0, 1, label_2);
		label_2.setSize("16", "16");
		label_2.setStyleName("gwt-PhotoViewerButton-Over");
		label_2.addMouseListener(new PhotoWidgetMouseListener(this, label_2, 1));

		final Image image = new Image();
		image.setUrl("lightbox/close.gif");
		image.setStyleName("gwt-Photo");
		PhotoWidgetListeners.setImageSize(image);
		horizontalPanel.add(image);
	}
	public PhotoWidget(String imageLoc) {
		init(imageLoc);
	}
	private void init(String imageLoc){
		verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);

		horizontalPanel = new HorizontalPanel();
		verticalPanel.add(horizontalPanel);
		horizontalPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel.setSize("330px", "250px");
		horizontalPanel.setSpacing(3);

		imageLocs = imageLoc.split(", ");		
		int n = imageLocs.length;

		if(n > 1){
			
			labels = new Label[n];
			images = new Image[n];
	
			grid = new Grid();
			verticalPanel.add(grid);
			grid.setSize("100%", "100%");
			//grid.setCellSpacing(5);
	
			int colsPerRow = 11; 
			int cols = (n-1)%colsPerRow+1; 
			if(n > 8) cols = colsPerRow;
			int rows = (n-1)/colsPerRow+1;
			grid.resize(rows, colsPerRow);
	
//			HorizontalPanel hidden = new HorizontalPanel();
//			hidden.setVisible(false);
//			horizontalPanel.add(hidden);
			
			for(int i = 0; i < n; ++i){
				int col = i%colsPerRow;
				int row = i/colsPerRow; 
				//Window.alert("col, row = " + Integer.toString(col) + ", " + Integer.toString(row));
				labels[i] = new Label("");
				labels[i].setSize("16px", "16px");
				grid.setWidget(row, col, labels[i]);	
				labels[i].setStyleName("gwt-PhotoViewerButton");
				labels[i].addMouseListener(new PhotoWidgetMouseListener(this, labels[i], i));
	
				images[i] = new Image();
				images[i].setUrl("/image/med?path=/"+imageLocs[i]);
				//hidden.add(images[i]);
				images[i].setStyleName("gwt-Photo");
				//PhotoWidgetListeners.setImageSize(images[i]);
				images[i].addLoadListener(new PhotoWidgetLoadListener(images[i]));
				//setImage(i);
			}
			PhotoWidgetListeners.setImageSize(images[0]);
			setImage(0);
			if(n < colsPerRow){
				for(int i = n; i < colsPerRow; ++i){
					//Window.alert("col = " + Integer.toString(i));
					final Label label = new Label("");
					label.setSize("16px", "16px");
					grid.setWidget(0, i, label);	
					label.setStyleName("gwt-PhotoViewerButtonEmpty");
				}				
			}
		}
		else {
			if(!imageLocs[0].equals(""))
				image = new Image("/image/med?path=/"+imageLocs[0]);
			else
				image = new Image("/image/med?path=/"+"mainimages/01_11a.jpg");
			//image.addMouseListener(new PhotoMouseListener(image));
			//image.setSize("260px", "260px");
			image.setStyleName("gwt-Photo");
			//image.addLoadListener(new PhotoWidgetLoadListener(image));

			//PhotoWidgetListeners.setImageSize(image);
			horizontalPanel.add(image);
			horizontalPanel.setCellVerticalAlignment(image, HasVerticalAlignment.ALIGN_MIDDLE);
			horizontalPanel.setCellHorizontalAlignment(image, HasHorizontalAlignment.ALIGN_CENTER);
		}
	}
	public void setImage(int i){
		try {
			horizontalPanel.remove(image);
		}
		catch(Exception e){}
			
		image = images[i];
		horizontalPanel.insert(image, 0);
		horizontalPanel.setCellVerticalAlignment(image, HasVerticalAlignment.ALIGN_MIDDLE);
		horizontalPanel.setCellHorizontalAlignment(image, HasHorizontalAlignment.ALIGN_CENTER);
	}
	public Image getImage(){return image;}
	public int getCurrent(){return current;}
	public void setCurrent(int c){
		current = c;
		for(int i = 0; i < labels.length; i++)
			labels[i].setStyleName("gwt-PhotoViewerButton");
		labels[current].setStyleName("gwt-PhotoViewerButton-over");
		setImage(current);
	}
	private VerticalPanel verticalPanel;
	private HorizontalPanel horizontalPanel;	
	private Grid grid;
	private Image image;
	private Image images[];
	private String imageLocs[];
	private int current = 0;
	private Label labels[];
}
